#include "../../include/core/Executor.hpp"
#include "../../include/command/CmderFactory.hpp"
#include <stdexcept>

// 构造函数：初始化默认状态（0,0,N，无加速/倒车）
Executor::Executor() 
    : currentPose({0, 0, 'N'}), isAccelerated(false), isReversed(false) {}

// 自定义初始化
void Executor::init(int32_t x, int32_t y, char heading) {
    currentPose.x = x;
    currentPose.y = y;
    if (heading != 'N' && heading != 'E' && heading != 'S' && heading != 'W') {
        throw std::invalid_argument("无效朝向：仅支持 N/E/S/W");
    }
    currentPose.heading = heading;
    isAccelerated = false;
    isReversed = false;
}

// 获取当前状态
Pose Executor::getCurrentState() const {
    return currentPose;
}

// 与测试代码匹配的接口实现
bool Executor::IsAccelerated() const {
    return isAccelerated;
}

bool Executor::IsReversed() const {
    return isReversed;
}

// 执行单个命令对象
void Executor::executeCommand(const std::unique_ptr<ICommand>& cmd) {
    if (cmd) {
        cmd->execute(*this);
    }
}

// 解析并执行单个字符指令
void Executor::executeCmd(char cmdChar) {
    auto cmd = CmderFactory::createCommand(cmdChar);
    if (!cmd) {
        throw std::invalid_argument("无效指令：仅支持 M/L/R/F/B/TR");
    }
    executeCommand(cmd);
}

// 执行批量字符指令（vector版本）
void Executor::executeBatchCmds(const std::vector<char>& cmds) {
    for (char c : cmds) {
        executeCmd(c);
    }
}

// 执行批量字符指令（string版本，支持TR解析）
void Executor::executeBatchCmds(const std::string& cmdStr) {
    std::vector<char> parsedCmds;
    for (size_t i = 0; i < cmdStr.size(); ++i) {
        if (cmdStr[i] == 'T' && i + 1 < cmdStr.size() && cmdStr[i+1] == 'R') {
            parsedCmds.push_back('Z');
            ++i;
        } else {
            parsedCmds.push_back(cmdStr[i]);
        }
    }
    executeBatchCmds(parsedCmds);
}

// ----------------- 内部辅助函数实现 -----------------

void Executor::move(int steps) {
    int effectiveSteps = steps;
    if (isReversed) {
        effectiveSteps *= -1;
    }
    switch (currentPose.heading) {
        case 'N': currentPose.y += effectiveSteps; break;
        case 'E': currentPose.x += effectiveSteps; break;
        case 'S': currentPose.y -= effectiveSteps; break;
        case 'W': currentPose.x -= effectiveSteps; break;
    }
}

void Executor::turnLeft() {
    if (isReversed) {
        // 倒车状态下，左转指令执行右转操作
        switch (currentPose.heading) {
            case 'N': currentPose.heading = 'E'; break;
            case 'E': currentPose.heading = 'S'; break;
            case 'S': currentPose.heading = 'W'; break;
            case 'W': currentPose.heading = 'N'; break;
        }
    } else {
        // 正常状态下的左转
        switch (currentPose.heading) {
            case 'N': currentPose.heading = 'W'; break;
            case 'W': currentPose.heading = 'S'; break;
            case 'S': currentPose.heading = 'E'; break;
            case 'E': currentPose.heading = 'N'; break;
        }
    }
}

void Executor::turnRight() {
    if (isReversed) {
        // 倒车状态下，右转指令执行左转操作
        switch (currentPose.heading) {
            case 'N': currentPose.heading = 'W'; break;
            case 'W': currentPose.heading = 'S'; break;
            case 'S': currentPose.heading = 'E'; break;
            case 'E': currentPose.heading = 'N'; break;
        }
    } else {
        // 正常状态下的右转
        switch (currentPose.heading) {
            case 'N': currentPose.heading = 'E'; break;
            case 'E': currentPose.heading = 'S'; break;
            case 'S': currentPose.heading = 'W'; break;
            case 'W': currentPose.heading = 'N'; break;
        }
    }
}